import os,sys,random,pygame,math
from pygame.locals import *
import cPickle as pickle

import screen,levels,player

class MainMenu:
    def __init__(self):
        pygame.init()
        pygame.mixer.set_num_channels(100)

        ## starts
        self.__game__init__()
        self.player = player.Player()


    def game(self):
        running = 1
        in_level = 1
        self.__level__init__(self.W1L1)
        while running:
            pygame.time.Clock().tick(60)
            screen.screen.fill((0,0,0))
            if in_level==1:
                self.__game_800(self.W1L1)

            pygame.display.flip()
            for event in pygame.event.get():
                if event.type == QUIT:
                    running = 0
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        running = 0

        pygame.quit()

    def __game__init__(self):
        self.W1L1 = levels.W1L1()

    def __level__init__(self,level):
        self.player.start_pos(level.start_pos())

    def __resize_screen(self):
        pygame.quit()
        pygame.init()
        pygame.mixer.set_num_channels(100)

    def __toggle_fullscreen(self):
        screen.screen.toggle_fullscreen()

    def __reset(self,level):
        self.__death()
        self.player.start_pos(level.start_pos())
        level.reset()

    def __death(self):
        for x in range(30):
            pygame.time.Clock().tick(60)
            screen.screen.fill((220,40,25))
            pygame.display.flip()

    def __game_800(self,level):
        self.__player_scroll(level)
        self.__check_world(level)
        level.update()
        self.player.update(level.image,level.rect)
        if self.player.alive == 0:
            self.__reset(level)

    def __game_1024(self,level):
        pass

    def __player_scroll(self,level):
        if self.player.rect.center[0]<200: ## Left side of the screen
            self.__move_right(level)
        if self.player.rect.center[0]>600: ## Right Sid
            self.__move_left(level)
        if self.player.rect.center[1]<200: ## Top of scree
            self.__move_down(level)
        if self.player.rect.center[1]>400: ## Bottom are
            self.__move_up(level)

    def __move_right(self,level):
        move = (abs(self.player.speed),0)
        if level.rect.left>=0:
            pass
        else:
            self.__move_world(level,move)

    def __move_left(self,level):
        move = (-abs(self.player.speed),0)
        if level.rect.right<=800:
            pass
        else:
            self.__move_world(level,move)

    def __move_up(self, level):
        move = (0,-abs(self.player.falling_speed))
        if level.rect.bottom<=600:
            pass
        else:
            self.__move_world(level,move)

    def __move_down(self, level):
        move = (0,abs(self.player.falling_speed))
        if level.rect.top>=0:
            pass
        else:
            self.__move_world(level,move)

    def __check_world(self,level):
        x = 0
        y = 0
        if level.rect.top > 0:
            y = -abs(level.rect.top)
        elif level.rect.bottom < 600:
            y = abs(600-level.rect.bottom)
        if level.rect.left > 0:
            x = -abs(level.rect.left)
        elif level.rect.right < 800:
            x = abs(800-level.rect.right)

        move = (x,y)
        self.__move_world(level,move)

    def __move_world(self,level,move):
        self.player.world_moves(move)
        level.move(move)
        
